package com.pricl.biz.util;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.springframework.web.multipart.MultipartFile;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadVideoRequest;
import com.aliyun.vod.upload.resp.UploadVideoResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.pricl.biz.file.entity.FileInfo;

/**
 * 阿里云视频服务的操作工具
 * @author Administrator
 *
 */
public class ALVideo {

	private static DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", OSSUtils.getOSSClient().getAccessKeyId(), OSSUtils.getOSSClient().getAccessKeySecret());
	public static Map<String,Object> uploadVideo(FileInfo item, InputStream in, long length,MultipartFile file) {
			Map<String,Object> map = new HashMap<String,Object>();
			String accessKeyId = "accessKeyId";         //帐号AK
	        String accessKeySecret = "accessKeySecret"; //帐号AK
	        String fileName = "/*/*/文件名称.mp4";      //指定上传文件绝对路径(文件名称必须包含扩展名)
	        String title = "视频标题";                  //视频标题
	        
	        accessKeyId= OSSUtils.getOSSClient().getAccessKeyId();
	        accessKeySecret=OSSUtils.getOSSClient().getAccessKeySecret();
	        fileName = file.getOriginalFilename();
	        title = file.getName();
	        System.out.println("accessKeyId:"+accessKeyId);
	        System.out.println("accessKeySecret:"+accessKeySecret);
	        System.out.println("fileName:"+fileName);
	        System.out.println("title:"+title);
	        UploadVideoRequest request = new UploadVideoRequest(accessKeyId, accessKeySecret, title, fileName);
	        request.setCateId(0);                                         //视频分类ID
	        request.setTags("标签1,标签2");                               //视频标签,多个用逗号分隔
	        request.setDescription("视频描述");                           //视频描述
	        request.setCoverURL("http://cover.sample.com/sample.jpg");    //视频自定义封面URL
	        request.setCallback("http://callback.sample.com");            //设置上传完成后的回调URL
	        request.setPartSize(10 * 1024 * 1024L);     //可指定分片上传时每个分片的大小，默认为10M字节
	        request.setTaskNum(1);                      //可指定分片上传时的并发线程数，默认为1，(注：该配置会占用服务器CPU资源，需根据服务器情况指定）
	        //request.setIsShowWaterMark(true);           //是否使用水印
	        try {
	            UploadVideoImpl uploader = new UploadVideoImpl();
	            UploadVideoResponse response = uploader.uploadVideo(request);
	            DefaultAcsClient client = new DefaultAcsClient(profile);
	            GetVideoPlayAuthResponse pAuthResponse = getVideoPlayAuth(client,response.getVideoId());
	            map.put("videoId", response.getVideoId());
	            map.put("playAuth", pAuthResponse.getPlayAuth());
	            System.out.println("——————————————————————————————————阿里云的上传成功后返回视频ID——————————————————————————————————");
	            System.out.print(response.getVideoId()); //上传成功后返回视频ID
	            return map;
	        } catch (Exception e) {
	            e.printStackTrace();
	            return map;
	        }
	}
	
	
	public static GetVideoPlayAuthResponse getVideoPlayAuth(DefaultAcsClient client,String videoId) {
	    GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
	    //request.setVideoId("视频ID");
	    request.setVideoId(videoId);
	    GetVideoPlayAuthResponse response = null;
	    try {
	        response = client.getAcsResponse(request);
	    } catch (ServerException e) {
	        throw new RuntimeException("GetVideoPlayAuthRequest Server failed");
	    } catch (ClientException e) {
	        throw new RuntimeException("GetVideoPlayAuthRequest Client failed");
	    }
	    response.getPlayAuth();              //播放凭证
	    response.getVideoMeta();             //视频Meta信息
	    System.out.println("——————————————————————————————————阿里云的视频——————————————————————————————————");
	    System.out.println("response.getPlayAuth()"+response.getPlayAuth());
	    System.out.println("response.getVideoMeta()"+response.getVideoMeta());
	    return response;
	}
	
	
}
